﻿using NUnit.Framework;
using Validate.Extensions;

namespace Validate.UnitTests
{
    [TestFixture]
    public class ValidatorTests_EqualTo
    {
        [Test]
        public void ShouldPassForEqualTo()
        {
            var person = new Person {Name = "Some Name", Age = 18};
            var validator = person.Validate().IsEqualTo(v => v.Age, 18, "Age should be 18");
            Assert.IsTrue(validator.IsValid);
        }

        [Test]
        public void ShouldFailForEqualTo()
        {
            var person = new Person { Name = "Some Name", Age = 18 };
            var validator = person.Validate().IsEqualTo(v => v.Age, 25, "Age should be 18");
            Assert.IsFalse(validator.IsValid);
        }

        [Test]
        public void ShouldBeAbleToVerifyAutoGeneratedValidationMessage1()
        {
            var person = new Person { Name = "Some Name", Age = 18, HomeAddress = new Address{City = "Reading"}};
            var validator = person.Validate(new ValidationOptions{StopOnFirstError = false})
                .IsEqualTo(v => v.Age, 25)
                .IsEqualTo(v => v.HomeAddress.City, "London");
            Assert.That(validator.Errors[0].Message, Is.EqualTo("Person.Age should be equal to 25."));
            Assert.That(validator.Errors[1].Message, Is.EqualTo("Address.City should be equal to London."));
        }

        [Test]
        public void ShouldPassForEqualToOtherMember()
        {
            var person = new Person { Name = "Some Name", NickName = "Some Name"};
            var validator = person.Validate().IsEqualTo(v => v.Name, v => v.NickName, "Name should be same as NickName");
            Assert.IsTrue(validator.IsValid);
        }

        [Test]
        public void ShouldFailForEqualToOtherMember()
        {
            var person = new Person { Name = "Some Name", NickName = "NickName" };
            var validator = person.Validate().IsEqualTo(v => v.Name, v => v.NickName, "Name should be same as NickName");
            Assert.IsFalse(validator.IsValid);
        }

        [Test]
        public void ShouldBeAbleToVerifyAutoGeneratedValidationMessage2()
        {
            var person = new Person { Name = "Some Name", NickName = "NickName" };
            var validator = person.Validate().IsEqualTo(v => v.Name, v => v.NickName);
            Assert.IsFalse(validator.IsValid);
            Assert.That(validator.Errors[0].Message, Is.EqualTo("Person.Name should be equal to Person.NickName."));
        }
    }
}